// pages/settings/settings.js
//获取应用实例
// const AV = require('../../utils/av-weapp-min');
// const { User } = require('../../utils/av-weapp-min');

var app = getApp()
Page({
  data:{
    user: null,
    userInfo: {},
    employeeId: '',
    inputStatus: false,
    username:'绑定账户',
    index: 0,              
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
    
  },
  onLoad:function(options){
    
  },
  onShow:function(){
    // 页面显示
    var that = this;
    var admin = wx.getStorageSync('admin')
    if (admin) {
      that.setData({
        username: admin
      })
    }
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {        
        that.setData({
          user: User.current(),
          userInfo: res.data
        })
      }
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      employeeId: e.detail.value
    })
  },
  save:function(){
    var selectedLanguage = app.globalData.settings.language;
    var title = ["已保存", "Saved", "保存完了"][selectedLanguage];
    wx.setStorageSync('employeeId', this.data.employeeId);
    app.globalData.settings.employeeId = this.data.employeeId;
    wx.showToast({      
      title: title,
      icon: "success",
      duration: 1500
    })
    // 更新视图
    this.setData({
      inputStatus: true,
      employeeId: this.data.employeeId
    })
  },
  unbind:function(){
    var that = this;
    var index = that.data.index;
    var UI = that.data.UI;
    var title = UI[index].unbindMsgTitle;
    var content = UI[index].unbindMsgContent;
    
    wx.showModal({
      title: title,
      content: content,
      success: function(res) {
        if (res.confirm) {
          console.log(AV.User.current());
          AV.User.logOut().then(console.log('Log out'));
          
          console.log(AV.User.current());
          that.setData({
            user: AV.User.current()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }

})